package GUI;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Point;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import java.awt.Dialog.ModalityType;
import javax.swing.DefaultComboBoxModel;

import com.sun.corba.se.impl.ior.NewObjectKeyTemplateBase;

import Xuly.MyColumn;
import Xuly.MyTable;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.concurrent.ArrayBlockingQueue;

public class CreateColumnDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtColumnName;
	private JTextField txtValue;
	public MyColumn myColumn;
	private MyTable myTable;
	private Point pointThis;
	public String flagEdit = "";

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			CreateColumnDialog dialog = new CreateColumnDialog(new MyTable("",
					new ArrayList<MyColumn>()), new Point(400, 200));
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public CreateColumnDialog(MyTable table, Point point) {

		myTable = table;

		setModalityType(ModalityType.APPLICATION_MODAL);
		setModal(true);
		setResizable(false);
		setTitle("Add column");
		setBounds(point.x, point.y, 250, 206);

		pointThis = getLocation();

		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JLabel lblColumnName = new JLabel("Column name: ");
		lblColumnName.setBounds(10, 18, 77, 14);
		contentPanel.add(lblColumnName);

		JLabel lblValue = new JLabel("Value:");
		lblValue.setBounds(10, 49, 64, 14);
		contentPanel.add(lblValue);

		JLabel lblTypeValue = new JLabel("Type value:");
		lblTypeValue.setBounds(10, 77, 64, 14);
		contentPanel.add(lblTypeValue);

		final JComboBox cboTypeValue = new JComboBox();
		cboTypeValue.setModel(new DefaultComboBoxModel(new String[] { "String",
				"Number" }));
		cboTypeValue.setBounds(106, 74, 126, 20);
		contentPanel.add(cboTypeValue);

		txtColumnName = new JTextField();
		txtColumnName.setBounds(106, 18, 126, 20);
		contentPanel.add(txtColumnName);
		txtColumnName.setColumns(10);

		txtValue = new JTextField();
		txtValue.setText("");
		txtValue.setBounds(106, 46, 126, 20);
		contentPanel.add(txtValue);
		txtValue.setColumns(10);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						if (txtColumnName.getText().equals("")) {
							MessageDialog messageDialog = new MessageDialog(
									"Nhập column name!", new Point(
											pointThis.x + 50, pointThis.y + 50));
							messageDialog.setVisible(true);
							return;
						}
						if (txtColumnName.getText().contains(" ")) {
							MessageDialog messageDialog = new MessageDialog(
									"Coulumn name không được có khoảng trắng. Nhập lại!",
									new Point(pointThis.x + 50,
											pointThis.y + 50));
							messageDialog.setVisible(true);
							return;
						}
						if (ktLaSo(txtColumnName.getText().toString())) {
							MessageDialog messageDialog = new MessageDialog(
									"Coulumn name chứa kí tự số. Nhập lại!",
									new Point(pointThis.x + 50,
											pointThis.y + 50));
							messageDialog.setVisible(true);
							return;
						}
						if (ktTrungColumnName()) {
							MessageDialog messageDialog = new MessageDialog(
									"Coulumn name bị trùng. Nhập lại!",
									new Point(pointThis.x + 50,
											pointThis.y + 50));
							messageDialog.setVisible(true);
							return;
						}

						if (txtValue.getText().equals("")) {
							MessageDialog messageDialog = new MessageDialog(
									"Nhập value!", new Point(pointThis.x + 50,
											pointThis.y + 50));
							messageDialog.setVisible(true);
							return;
						}
						if (cboTypeValue.getSelectedIndex() < 0) {
							MessageDialog messageDialog = new MessageDialog(
									"Chọn type value!", new Point(
											pointThis.x + 50, pointThis.y + 50));
							messageDialog.setVisible(true);
							return;
						}
						if (cboTypeValue.getSelectedItem().toString() == "Number") {
							try {
								Double.parseDouble(txtValue.getText()
										.toString());
							} catch (Exception e2) {
								MessageDialog messageDialog = new MessageDialog(
										"Value không hợp lệ!", new Point(
												pointThis.x + 50,
												pointThis.y + 50));
								messageDialog.setVisible(true);
								return;
							}
						}

						myColumn = new MyColumn(txtColumnName.getText(), cboTypeValue
										.getSelectedItem().toString());

						txtColumnName.setText("");
						txtValue.setText("");
						cboTypeValue.setSelectedIndex(0);
						setVisible(false);
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						
						myColumn = null;
						setVisible(false);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	Boolean ktTrungColumnName() {
		if(myTable == null)
			return false;
		if(flagEdit == txtColumnName.getText().toString())
			return false;
		for (int i = 0; i < myTable.getColumns().size(); i++) {
			if (myTable.getColumns().get(i).getName()
					.equals(txtColumnName.getText().toString()))
				return true;
		}
		return false;
	}

	Boolean ktLaSo(String st) {
		if (st.contains("1"))
			return true;
		if (st.contains("2"))
			return true;
		if (st.contains("3"))
			return true;
		if (st.contains("4"))
			return true;
		if (st.contains("5"))
			return true;
		if (st.contains("6"))
			return true;
		if (st.contains("7"))
			return true;
		if (st.contains("8"))
			return true;
		if (st.contains("9"))
			return true;

		if (st.contains("0"))
			return true;

		return false;
	}
}
